package com.ljf.tijian.service;

import com.ljf.tijian.dto.ApiResponse;
import com.ljf.tijian.exception.TijianAppException;
import com.ljf.tijian.exception.TijianExceptionEnum;
import com.ljf.tijian.mapper.DoctorMapper;
import com.ljf.tijian.pojo.Doctor;
import com.ljf.tijian.service.Impl.DocterServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DocterService implements DocterServiceImpl {

    @Autowired
    DoctorMapper doctorMapper;

    @Value("${salt}")
    String salt;

    public ApiResponse login(Doctor doctor){

        Doctor docterRes = doctorMapper.selectDocterByCode(doctor);

        if(docterRes == null){
//            return new ApiResponse(201,"该用户还未注册");
//            throw new TijianAppException(201,"用户还未注册");
            throw new TijianAppException(TijianExceptionEnum.MOBILE_NOT_REGISTER.getCode(),TijianExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }else{
//            String salt = "nvfkahh321dsa";
            String originl_pwd = doctor.getPassword()+salt;
            String final_pwd = DigestUtils.md5Hex(originl_pwd.getBytes());
            System.out.println(final_pwd);
            if(docterRes.getPassword().equals(final_pwd)){
                return new ApiResponse(200,"LOGIN SUCCESS",docterRes);
            }else {
//                return new ApiResponse(202,"密码错误");
//                throw new TijianAppException(202,"密码错误");
                throw new TijianAppException(TijianExceptionEnum.PASSWORD_IS_WRONG.getCode(),TijianExceptionEnum.PASSWORD_IS_WRONG.getMessage());
            }
        }
    }

}
